﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Collections;
using System.Xml;
namespace ConditionDrawing.Controllers
{
    public class drawController : Controller
    {
        //ActionResult
        // GET: /draw/Index
        [HttpPost]
        public void Element()
        {
            Response.ContentType = "text/plain";
           
            String txt = Request["state"].ToString();
            string json = "";
            DataTable tblDatas = new DataTable("Datas");
            if (txt == "write")
            {

                tblDatas.Columns.Add("style", Type.GetType("System.String"));
                tblDatas.Columns.Add("left", Type.GetType("System.String"));
                tblDatas.Columns.Add("top", Type.GetType("System.String"));

                String txt1 = Request["style"].ToString();
                String txt2 = Request["left"].ToString();
                String txt3 = Request["top"].ToString();
                ArrayList p1 = new ArrayList();

                ArrayList p2 = new ArrayList();

                ArrayList p3 = new ArrayList();
                p1 = insert(txt1);

                p2 = insert(txt2);
                p3 = insert(txt3);
                var k = num(txt1);
                for (var t = 0; t < k; t++)
                {
                    tblDatas.Rows.Add(new object[] { p1[t], p2[t], p3[t] });
                }
                json = DataTableConvertJson.DataTableToJson("tble", tblDatas);





                //创建一个XmlDocument对象
                XmlDocument doc = new XmlDocument();
                doc.RemoveAll();

                doc.LoadXml("<target> " + json + "</target>");
                //保存这个文档到文件中
                doc.Save("D:\\新建文件夹 (3)\\data.xml");





            }
            else
            {
                XmlDocument doc = new XmlDocument();
                // 装入指定的XML文档
                doc.Load("D:\\新建文件夹 (3)\\data.xml");

                json = ((doc).DocumentElement).InnerText;


            }


            Response.Write(json);



            //return View(json);
        }
        // POST: /drawing/
        [HttpPost]
        public void line()
        {
            Response.ContentType = "text/plain";
            String txt = Request["states"].ToString();
            string json = "";
            DataTable tblDatas = new DataTable("Datas");
            if (txt == "write")
            {

                tblDatas.Columns.Add("sl", Type.GetType("System.String"));
                tblDatas.Columns.Add("st", Type.GetType("System.String"));
                tblDatas.Columns.Add("el", Type.GetType("System.String"));
                tblDatas.Columns.Add("et", Type.GetType("System.String"));

                String txt1 = Request["sl"].ToString();
                String txt2 = Request["st"].ToString();
                String txt3 = Request["el"].ToString();
                String txt4 = Request["et"].ToString();
                ArrayList p1 = new ArrayList();
                ArrayList p2 = new ArrayList();
                ArrayList p3 = new ArrayList();
                ArrayList p4 = new ArrayList();
                p1 = insert(txt1);

                p2 = insert(txt2);
                p3 = insert(txt3);
                p4 = insert(txt4);
                var k = num(txt1);
                for (var t = 0; t < k; t++)
                {
                    tblDatas.Rows.Add(new object[] { p1[t], p2[t], p3[t], p4[t] });
                }
                json = DataTableConvertJson.DataTableToJson("tble", tblDatas);

                //创建一个XmlDocument对象
                XmlDocument doc = new XmlDocument();
                doc.RemoveAll();
                doc.LoadXml("<target> " + json + "</target>");
                //保存这个文档到文件中
                doc.Save("D:\\新建文件夹 (3)\\data2.xml");
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                // 装入指定的XML文档
                doc.Load("D:\\新建文件夹 (3)\\data2.xml");
                json = ((doc).DocumentElement).InnerText;
            }
            Response.Write(json);
        }
       
        

        public ArrayList insert(String txt)
        {
            ArrayList p3 = new ArrayList();
            var s3 = txt.ToList();
            var pp3 = "";
            for (var t = 0; t < txt.Length; t++)
            {

                if (s3[t].ToString() == " ")
                {
                    p3.Add(pp3);
                    pp3 = "";

                }
                else
                {
                    pp3 += s3[t].ToString();
                }
            }
            return p3;
        }
        public int num(String txt)
        {
            ArrayList p3 = new ArrayList();
            var s3 = txt.ToList();
            var k3 = 0;

            for (var t = 0; t < txt.Length; t++)
            {

                if (s3[t].ToString() == " ")
                {
                    k3 += 1;
                }

            }
            return k3;
        }
        
    }
}
